package com.atguigu.yygh.cmn.controller.admin;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-09-23
 */
@Api("数据字典管理")
@RestController
@RequestMapping("/admin/cmn")
//@CrossOrigin
public class DictController {

    @Autowired
    DictService dictService;


    @ApiOperation("根据父节点查询子节点数组")
    @GetMapping("/getList/{pid}")
    public R getDictListByParentId(@PathVariable Long pid){
        List<Dict> dictList = dictService.getDictListByParentId(pid);
        return R.ok().data("list",dictList);
    }

    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }
    
    @PostMapping("/importData")
    public R importData(MultipartFile file) throws IOException {
        dictService.importData(file);
        return R.ok();
    }

    @GetMapping("/getNameByValue/{value}")
    public String getNameByValue(@PathVariable("value") String value){
        Dict dict = dictService.getNameByParentCodeAndValue(null,value);
        return dict.getName();
    }

    @GetMapping("/getNameByParentCodeAndValue/{parentCode}/{value}")
    public String getNameByParentCodeAndValue(@PathVariable("parentCode") String parentCode ,@PathVariable("value") String value){
        Dict dict = dictService.getNameByParentCodeAndValue(parentCode,value);
        return dict.getName();
    }

}

